//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdBlackScholesAsianEngine.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL::Processes;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesAsianEngine::CFdBlackScholesAsianEngine (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Microsoft::FSharp::Core::FSharpOption<UInt64>^ tGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ xGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ aGrid, Microsoft::FSharp::Core::FSharpOption<Double>^ theta) : CPricingEngine(CFdBlackScholesAsianEngine::typeid)
{
    CGeneralizedBlackScholesProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phFdBlackScholesAsianEngine = NULL;
#endif
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetShared ()); 
        QuantLib::Size _tGrid = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (tGrid) ? (QuantLib::Size)ValueHelper::Convert (tGrid->Value) : 100); //4
        QuantLib::Size _xGrid = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (xGrid) ? (QuantLib::Size)ValueHelper::Convert (xGrid->Value) : 100); //4
        QuantLib::Size _aGrid = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (aGrid) ? (QuantLib::Size)ValueHelper::Convert (aGrid->Value) : 50); //4
        QuantLib::Real _theta = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (theta) ? (QuantLib::Real)ValueHelper::Convert (theta->Value) : 0.5); //4
        _ppFdBlackScholesAsianEngine = new boost::shared_ptr<QuantLib::FdBlackScholesAsianEngine> (new QuantLib::FdBlackScholesAsianEngine ( _process,  _tGrid,  _xGrid,  _aGrid,  _theta ));
        SetPricingEngine (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdBlackScholesAsianEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesAsianEngine::CFdBlackScholesAsianEngine (boost::shared_ptr<QuantLib::FdBlackScholesAsianEngine>& childNative, Object^ owner) : CPricingEngine(CFdBlackScholesAsianEngine::typeid)
{
#ifdef HANDLE
	_phFdBlackScholesAsianEngine = NULL;
#endif
	_ppFdBlackScholesAsianEngine = &childNative;
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdBlackScholesAsianEngine));
}
Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesAsianEngine::CFdBlackScholesAsianEngine (QuantLib::FdBlackScholesAsianEngine& childNative, Object^ owner) : CPricingEngine(CFdBlackScholesAsianEngine::typeid)
{
#ifdef HANDLE
	_phFdBlackScholesAsianEngine = NULL;
#endif
	_ppFdBlackScholesAsianEngine = new boost::shared_ptr<QuantLib::FdBlackScholesAsianEngine> (&childNative);
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdBlackScholesAsianEngine));
    _FdBlackScholesAsianEngineOwner = owner;
    _PricingEngineOwner = owner;
}

Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesAsianEngine::CFdBlackScholesAsianEngine (CFdBlackScholesAsianEngine^ copy) : CPricingEngine(CFdBlackScholesAsianEngine::typeid)
{
#ifdef HANDLE
	_phFdBlackScholesAsianEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdBlackScholesAsianEngine = new boost::shared_ptr<QuantLib::FdBlackScholesAsianEngine> (copy->GetShared());
        _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdBlackScholesAsianEngine));
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesAsianEngine::CFdBlackScholesAsianEngine (System::Type^ t) : CPricingEngine(CFdBlackScholesAsianEngine::typeid)
{
#ifdef HANDLE
	_phFdBlackScholesAsianEngine = NULL;
#endif
	if (!t->IsSubclassOf(CFdBlackScholesAsianEngine::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesAsianEngine::CFdBlackScholesAsianEngine (QuantLib::Handle<QuantLib::FdBlackScholesAsianEngine>& childNative, Object^ owner)  : CPricingEngine(CFdBlackScholesAsianEngine::typeid)
{
	_phFdBlackScholesAsianEngine = &childNative;
	_ppFdBlackScholesAsianEngine = &static_cast<boost::shared_ptr<QuantLib::FdBlackScholesAsianEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdBlackScholesAsianEngine));
    _FdBlackScholesAsianEngineOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesAsianEngine::CFdBlackScholesAsianEngine (QuantLib::Handle<QuantLib::FdBlackScholesAsianEngine> childNative)  : CPricingEngine(CFdBlackScholesAsianEngine::typeid)
{
	_phFdBlackScholesAsianEngine = &childNative;
	_ppFdBlackScholesAsianEngine = &static_cast<boost::shared_ptr<QuantLib::FdBlackScholesAsianEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdBlackScholesAsianEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesAsianEngine::CFdBlackScholesAsianEngine (QuantLib::FdBlackScholesAsianEngine childNative)  : CPricingEngine(CFdBlackScholesAsianEngine::typeid)
{
#ifdef HANDLE
	_phFdBlackScholesAsianEngine = NULL;
#endif
	_ppFdBlackScholesAsianEngine = new boost::shared_ptr<QuantLib::FdBlackScholesAsianEngine> (new QuantLib::FdBlackScholesAsianEngine (childNative));
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdBlackScholesAsianEngine));
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesAsianEngine::~CFdBlackScholesAsianEngine ()
{
    if (_ppFdBlackScholesAsianEngine != NULL)
    {
	    delete _ppFdBlackScholesAsianEngine;
        _ppFdBlackScholesAsianEngine = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesAsianEngine::!CFdBlackScholesAsianEngine ()
{
    if (_ppFdBlackScholesAsianEngine != NULL)
    {
	    delete _ppFdBlackScholesAsianEngine;
    }
}
QuantLib::FdBlackScholesAsianEngine& Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesAsianEngine::GetReference ()
{
    if (_ppFdBlackScholesAsianEngine == NULL) throw gcnew NativeNullException ();
	return **_ppFdBlackScholesAsianEngine;
}
boost::shared_ptr<QuantLib::FdBlackScholesAsianEngine>& Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesAsianEngine::GetShared ()
{
    if (_ppFdBlackScholesAsianEngine == NULL) throw gcnew NativeNullException ();
	return *_ppFdBlackScholesAsianEngine;
}
QuantLib::FdBlackScholesAsianEngine* Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesAsianEngine::GetPointer ()
{
    if (_ppFdBlackScholesAsianEngine == NULL) throw gcnew NativeNullException ();
	return &**_ppFdBlackScholesAsianEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdBlackScholesAsianEngine>& Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesAsianEngine::GetHandle ()
{
	if (_phFdBlackScholesAsianEngine == NULL)
	{
		_phFdBlackScholesAsianEngine = new Handle<QuantLib::FdBlackScholesAsianEngine> (*_ppFdBlackScholesAsianEngine);
	}
	return *_phFdBlackScholesAsianEngine;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesAsianEngine::HasNative () 
{
	return (_ppFdBlackScholesAsianEngine != NULL);
}

Cephei::QL::Experimental::Finitedifferences::IFdBlackScholesAsianEngine^ Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesAsianEngine::Calculate::get ()
{
    try
    {
    	(*_ppFdBlackScholesAsianEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFdBlackScholesAsianEngine^ Cephei::QL::Experimental::Finitedifferences::CFdBlackScholesAsianEngine_Factory::Create (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Microsoft::FSharp::Core::FSharpOption<UInt64>^ tGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ xGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ aGrid, Microsoft::FSharp::Core::FSharpOption<Double>^ theta)
{
    return gcnew CFdBlackScholesAsianEngine ( process,  tGrid,  xGrid,  aGrid,  theta);
}
